﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class Admin_UserControls_GridPager : System.Web.UI.UserControl
{
    GridView Grid;
    protected void Page_Load(object sender, EventArgs e)
    {
        //GridViewRow gRow = this.NamingContainer as GridViewRow;        
        //Control ctrl = gRow;
        //while (ctrl.GetType() != typeof(GridView))
        //{
        //    if (ctrl.GetType() == typeof(Page))
        //    {
        //        break;
        //    }
        //    ctrl = ctrl.Parent;
        //}
        Control c = Parent;
        while (c != null)
        {
            if (c is GridView)
            {
                Grid = (GridView)c;
                break;
            }
            c = c.Parent;
        }
        //if (ctrl is GridView)
        //{
            //Grid = (GridView)ctrl;
            if (Grid.AllowPaging)
            {
                SetPager();
            }
            else
            {
                this.PagerPanel.Visible = false;
            }
        //}
    }

    void SetPager()
    {
        LinkButton first = new LinkButton();
        first.ID = "first";
        first.Text = "第一页";
        first.CommandName = "Page";
        first.CommandArgument = "First";
        first.CssClass = "PagerFirst";
        PagerPanel.Controls.Add(first);
        PagerPanel.Controls.Add(new Literal() { Text = "&nbsp;" });

        LinkButton prew = new LinkButton();
        prew.ID = "prev";
        prew.Text = "上一页";
        prew.CommandName = "Page";
        prew.CommandArgument = (Grid.PageIndex == 0) ? "First" : "Prev";
        prew.CssClass = "PagerPrew";        
        PagerPanel.Controls.Add(prew);
        PagerPanel.Controls.Add(new Literal() { Text = "&nbsp;" });

        //添加数字
        int cnt = Grid.PagerSettings.PageButtonCount;
        int front = cnt / 2;
        int back = front;
        if (cnt % 2 > 0) { back++; }        
        int start = Grid.PageIndex - front;
        int end = Grid.PageIndex + back;        
        if (start < 0)
        {            
            start = 0;
            end = start + cnt-1;
            end = Math.Min(end, Grid.PageCount-1);
        }
        else if (end > Grid.PageCount - 1)
        {            
            end = Grid.PageCount-1;
            start = end - cnt;
            start = Math.Max(0, start);
        }        
        for (int i = start; i <= end; i++)
        {
            LinkButton iLink = new LinkButton();
            iLink.CssClass = "PagerNum";
            iLink.Text = Convert.ToString(i + 1);

            if (i == Grid.PageIndex)
            {
                //iLink.ForeColor = Color.Red;
                iLink.CssClass = "PagerNowNum";
            }
            iLink.CommandName = "Page";            
            iLink.CommandArgument = (i+1).ToString();
            PagerPanel.Controls.Add(iLink);
            PagerPanel.Controls.Add(new Literal() { Text = "&nbsp;" });
        }

        //数字完

        LinkButton next = new LinkButton();
        next.ID = "next";
        next.Text = "下一页";
        next.CommandName = "Page";
        next.CommandArgument = (Grid.PageIndex == Grid.PageCount) ? "Last" : "Next";
        next.CssClass = "PagerNext";        
        PagerPanel.Controls.Add(next);
        PagerPanel.Controls.Add(new Literal() { Text = "&nbsp;" });

        LinkButton last = new LinkButton();
        last.ID = "last";
        last.Text = "最后页";
        last.CommandName = "Page";
        last.CommandArgument = "Last";
        last.CssClass = "PagerLast";        
        PagerPanel.Controls.Add(last);
        PagerPanel.Controls.Add(new Literal() { Text = "&nbsp;" });

        Label txt = new Label();
        txt.Text = "共" + Grid.PageCount.ToString() + "页/第" + Convert.ToString(Grid.PageIndex + 1) + "页";
        txt.CssClass = "PagerLabel";
        PagerPanel.Controls.Add(txt);
    }
}
